<?php

namespace app\index\controller;

use app\index\model\User as UserModel;
use app\index\model\Data;
use app\index\model\Profile;
use think\Loader;
use think\Db;
use think\Request;
use think\Controller;
use think\Session;

class User extends Controller
{
	public function show()
	{
		$this->assign('title','这里是默认标题');
		return $this->fetch();
	}
	public function login(UserModel $user)
	{
		//dump($_POST['password']);
		if(!empty($_POST)){
		$a = $_POST['username'];
		$b = $_POST['password'];
		$result = $user->where(['password'=>"$b",'username'=>"$a"])->value('uid');
		if(!empty($result)){
			$qq = $user->where(['username'=>"$a"])->value('qq');
			Session::set('user',$qq);
			Session::set('uid',$result);
			Session::set('username',$a);

			//dump($_SESSION['username']);
			$this->success('登陆成功',THINK_URL);
			} else {
			$this->success('密码错误');
			}
		}

		return $this->fetch();
	}
	public function session()
	{
		session(null);
		$this->success('退出成功',THINK_URL);
		//return $this->fetch('index/index');
	}
	public function login1(UserModel $user)
	{
		//dump($request->psot());
		if(empty($_POST['zhanghao'])){
			return false;
		}
		$result = $user->where(['username'=>$_POST['zhanghao']])->select();
		//var_dump($result);
		if(empty($result)){
			return false ;
		}
		return true;

	}
	public function register()
	{
		$username = request()->post('username');
		$password = request()->post('password');
		$email = request()->post('email');
		$code = request()->post('yzm');
		if(!empty($_POST['username'])){
				$user= new UserModel;
				$user->data([
					'username'=>$username,
					'password'=>$password,
					'email'=>$email,
					]);
				$user->save();
<<<<<<< HEAD
				$this->success('注册成功,请登录',THINK_URLL);
=======
				$this->success('注册成功,请登录','THINK_URL/index/user/login');
			} else {
				$this->success('验证码错误');
			}
>>>>>>> aab76ebd201a0f5b081aa923a0e4742fc9bb6485
		}
		return $this->fetch();
	}
	public function register1(UserModel $user)
	{	//检查数据库中是否存在此账户
		if(empty($_POST['zhanghao'])){
			return false;
		} else {$result = $user->where(['username'=>$_POST['zhanghao']])->select();
		//dump($result);
		if(empty($result)){
			return false ;
			}
		return true;
		}
	}
	public function register2(Request $request){

		if(empty($_POST['mima'])){
			//检查密码是否为空
			return false;
		} else {
			return true;
		}
	}
	public function register3(Request $request){
		if(empty($_POST['qrmima'])){
			//检查确认密码是否为空
			return false;
		} else {

			return $_POST['qrmima'] ;
		}
	}

/*
	public function oneToMany()
	{
		$user = UserModel::get(3);
		dump($user->artical);
	}


	public function oneToOne()
	{
		$user = UserModel::get(2);
		echo $user->profile()->save(
			['age'=>10,
			'sex'=>0,
			'wechat'=>'ad1dd']);
		$p = profile::get(1);
		echo $p->user->nickname;
	}


	public function auto(UserModel $user)
	{
		$user->save();
	}
	public function softDelete()
	{
		//$nihao = UserModel::get(6);
		//$nihao->delete(true);
		dump(UserModel::onlyTrashed()->find());
	}


	}
	public function autoTime(UserModel $user)
	{
		//$user->nickname = 'nihao';
		//$user->save();
		$u = $user->get(5);
		$u->save(['nickname'=>'你想干嘛']);
	}

	public function getter(){
		$u = Data::get(1);
		echo $u->getData('status');
		//$u->name = 'ABHello';
		//$u->save();
	}*/
	//测试模型
/*	public function model()
	{
		//静态调用

		$u = UserModel::get(1);
		$u->nickname = 'tom';
		$u->save();

		//实例化模型
		$u = new UserModel();
		$u->nickname = 'xiaohong';
		$u->save();

		$u = Loader::model('User');
		$u->nickname = 'huahua';
		$u->save();

		$u = model('User');
		$u->nickname = 'jingjing';
		$u->save();

		$u = model('User');
		$u->data(['nickname' => 'zhaoliying','email'=>'zly@xx.com']);
		$u->save();
		dump($u->id);

		$u = model('User');
		$u->nickname = 'abc';
		$u->save();
		$u->nickname = 'hello';
		$u->save();

		$u = UserModel::create(['nickname'=>'qiange']);
		dump($u->nickname);
	}
*/
	public function index()
	{
		dump(THINK_URL);
		return '调用了user控制器的index方法';
	}
}